#include <cstdio>
#include <deque>
using namespace std;
int status[70000]; //用来判重
int nums, nume;
struct STATUS
{
    int t[5][5];
    int step;
} sts[2]; //用结构体存棋盘和步数
int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
deque<STATUS> q; //队列queue的内部其实是deque...直接用deque可能快上那么一丢丢

bool check(STATUS temp)
{ //判重函数
    int num = 0;
    for (int i = 1; i <= 4; i++)
        for (int j = 1; j <= 4; j++)
            num = (num << 1) + temp.t[i][j]; //转十进制
    if (status[num] == 1)
        return false;
    else
        return status[num] = true;
}

bool cmp(STATUS a, STATUS b)
{ //比较两个棋盘是否相等
    for (int i = 1; i <= 4; i++)
        for (int j = 1; j <= 4; j++)
        {
            if (a.t[i][j] != b.t[i][j])
                return false;
        }
    return true;
}

void search()
{
    sts[0].step = 0;
    q.push_back(sts[0]);
    while (!q.empty())
    {
        STATUS curr = q.front();
        q.pop_front();
        for (int i = 1; i <= 4; i++)
            for (int j = 1; j <= 4; j++)
            {
                if (curr.t[i][j] == 1)
                {
                    for (int k = 0; k < 4; k++)
                    {
                        int kx = dx[k] + i;
                        int ky = dy[k] + j;
                        if (kx > 0 && ky > 0 && ky < 5 && kx < 5 && curr.t[kx][ky] == 0)
                        {
                            STATUS copy = curr;
                            swap(copy.t[i][j], copy.t[kx][ky]);
                            ++copy.step;
                            if (check(copy))
                            {
                                q.push_back(copy);
                                if (cmp(copy, sts[1]))
                                    return; //如果达到目标棋盘则直接return                            }
                            }
                        }
                    }
                }
            }
    }
}

void input()
{
    char c[6];
    nums = 0;
    nume = 0;
    for (int i = 1; i <= 4; i++)
    {
        scanf("%s", c);
        scanf("\n");
        for (int j = 0; j < 4; j++)
        {
            if (c[j] == '1')
                sts[0].t[i][j + 1] = 1;
            nums = (nums << 1) + c[j] - '0';
        }
    }
    status[nums] = 1;
    for (int i = 1; i <= 4; i++)
    {
        scanf("%s", c);
        if (i != 4)
            scanf("\n");
        for (int j = 0; j < 4; j++)
        {
            if (c[j] == '1')
                sts[1].t[i][j + 1] = 1;
            nume = (nume << 1) + c[j] - '0';
        }
    }
    status[nume] = 0;
}

int main()
{
    input();
    if (cmp(sts[1], sts[0]))
    { //特判一下起始棋盘和目标棋盘是否是一样的，不然会迷之WA
        printf("0");
        return 0;
    }
    search();
    printf("%d", q.back().step);
}